//
//  CarNumberView.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/28/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit



class CarNumberView: UIView {
    
    // MARK: - Outlets
    
    lazy private var numberLbl: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.sizeToFit()
        label.textColor = .darkBlueColor
        label.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        return label
    }()
    lazy private var lettersLbl: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.sizeToFit()
        label.textColor = .darkBlueColor
        label.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        return label
    }()
    
    lazy private var cityIdLbl: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.sizeToFit()
        label.textColor = .darkBlueColor
        label.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        return label
    }()
    lazy private var dividerLine: UIView = {
        let view = UIView()
        view.backgroundColor = .darkBlueColor
        return view
    }()
    
    
    init () {
        super.init(frame: .zero)
        self.addSubview(numberLbl)
        self.addSubview(lettersLbl)
        self.addSubview(cityIdLbl)
        self.layer.borderWidth = 1
        self.layer.borderColor = UIColor.darkBlueColor.cgColor
        self.layer.cornerRadius = 7
        self.backgroundColor = .white
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func updateUI (with carModel: CarModel) {
        self.numberLbl.text = carModel.carNumber
        self.lettersLbl.text = carModel.carLetters
        self.cityIdLbl.text = carModel.cityId
        
        if (carModel.isNewFormat) {
            self.addNewFormatConstraints()
        } else {
            self.addOldFormatConstraints()
        }
    }
    
    private func addNewFormatConstraints() {
        self.addSubview(dividerLine)
        
        numberLbl.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(12)
            make.centerY.equalToSuperview()
        }
        lettersLbl.snp.makeConstraints { (make) in
            make.leading.equalTo(numberLbl.snp.trailing).offset(8)
            make.centerY.equalToSuperview()
        }
        dividerLine.snp.makeConstraints { (make) in
            make.height.equalToSuperview()
            make.width.equalTo(1)
            make.leading.equalTo(lettersLbl.snp.trailing).offset(8)
        }
        cityIdLbl.snp.makeConstraints { (make) in
            make.trailing.equalToSuperview().offset(-12)
            make.centerY.equalToSuperview()
            make.width.equalTo(20)
            make.leading.equalTo(dividerLine.snp.trailing).offset(8)
        }
    }
    
    private func addOldFormatConstraints () {
        cityIdLbl.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(24)
            make.centerY.equalToSuperview()
        }
        numberLbl.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        lettersLbl.snp.makeConstraints { (make) in
            make.trailing.equalToSuperview().offset(-12)
            make.centerY.equalToSuperview()
        }
    }
    
}

